** This do-file extracts parliamentary election results from the Global elections database

// Importing election results from country files
foreach name in albania australia austria belgium bermuda bolivia bosnia-herzegovina botswana bulgaria canada colombia costa-rica croatia cyprus czech-republic czechoslovakia dominican-republic estonia finland france germany greece hungary iceland indonesia ireland israel italy latvia lithuania luxembourg malaysia malta mauritius mexico moldova netherlands new-zealand niger norway poland portugal romania russia slovakia slovenia solomon-islands south-africa spain sweden switzerland trinidad-and-tobago turkey united-kingdom united-states venezuela west-germany{
	import delimited using "$project_path/data/1_input/elections/GLOBAL/GlobalElections_`name'.csv", asdouble encoding(UTF-8) clear
	local country = subinstr("`name'", "-", "_", .)
	tempfile elections_`country'
	save `elections_`country''
}
use `elections_albania'
foreach name in australia austria belgium bermuda bolivia bosnia-herzegovina botswana bulgaria canada colombia costa-rica croatia cyprus czech-republic czechoslovakia dominican-republic estonia finland france germany greece hungary iceland indonesia ireland israel italy latvia lithuania luxembourg malaysia malta mauritius mexico moldova netherlands new-zealand niger norway poland portugal romania russia slovakia slovenia solomon-islands south-africa spain sweden switzerland trinidad-and-tobago turkey united-kingdom united-states venezuela west-germany{
	local country = subinstr("`name'", "-", "_", .)
	append using `elections_`country''
}
drop legid electiontotalvotes
ren cnty Country
ren year Year 
ren totalseats Total_Seats
ren seats Seats_
ren party Party_
gen double Seat_Share_ = Seats_/Total_Seats

// Cleaning party name
gen party_code = substr(Party_, 1, strpos(Party_, "-"))
replace Party_ = subinstr(Party_, party_code, "", .)
drop party_code
 
// Separate month from year variable (for some elections, the month is inaccurate and we correct it)
gen Month = .
replace Month = 2 if Year==1974.02
replace Month = 10 if Year==1974.12 & Country=="United Kingdom"
replace Month = 2 if Year==1982.02
replace Month = 11 if Year==1982.11
replace Month = 6 if Year==1989.07 & Country=="Greece"
replace Month = 11 if Year==1989.11
replace Year = 1974 if Year==1974.02 | Year==1974.12
replace Year = 1982 if Year==1982.02 | Year==1982.11
replace Year = 1989 if Year==1989.07 | Year==1989.11

// Keeping only elections between 1946 and 2018
keep if Year>1945 & Year<=2018

// Reshaping and ordering parties
gsort Country Year Month -Seats_ -votes
drop votes 
bysort Country Year Month : gen id = _n
reshape wide Party_ Seats_ Seat_Share_, i(Country Year Month) j(id)

// Dropping parties after n°75, after checking that no seats are associated with these parties
forvalues i=76/287 {
	assert Seats_`i'==0 | Seats_`i'==.
	drop Party_`i' Seats_`i' Seat_Share_`i'
}
gen Type_Election = "Parliamentary"
order Country Year Month Type_Election Total_Seats

// Adjusting country names
replace Country = "Germany" if Country=="West Germany"
replace Country = "Czech Rep." if Country=="Czechoslovakia"
replace Country="Bosnia-Herz"  if Country=="Bosnia-Herzegovina"
replace Country="Czech Rep."  if Country=="Czech Republic"
replace Country="Dom. Rep."  if Country=="Dominican Republic"
replace Country="FRG/Germany"  if Country=="Germany"
replace Country="Solomon Is."  if Country=="Solomon Islands"
replace Country="S. Africa"  if Country=="South Africa"
replace Country="Trinidad-Tobago"  if Country=="Trinidad and Tobago"
replace Country="UK"  if Country=="United Kingdom"
replace Country="USA"  if Country=="United States"

// Adjusting month variable when necessary
replace Month = 10 if Country=="Iceland" & Year==1959
replace Month = 4 if Country=="Iceland" & Year==1991

// Dropping by-elections
drop if Country=="New Zealand" & Year==1947
drop if Country=="New Zealand" & Year==1953
drop if Country=="New Zealand" & Year==1958
drop if Country=="New Zealand" & Year==1970

// Dropping observations without information on the total number of seats
drop if Total_Seats==0

// Adding flag variables
foreach v in flag_wrong_sum_seats flag_inconsequential flag_vacant_seats flag_vacant_seats_nb flag_appointed flag_appointed_nb flag_non_partisan flag_coup {
	gen `v' = .
}
gen flag_inconsequential_note = ""
order flag_inconsequential_note, after(flag_inconsequential)

// Detecting elections which have the wrong sum of seats
egen sum_seats_parties = rowtotal(Seats_*)
replace flag_wrong_sum_seats = 1 if sum_seats_parties!=Total_Seats
drop sum_seats_parties

lab var Country  "Country in which election took place"
lab var Year     "Year of the election"
lab var Month    "Month of the election"
lab var Type_Election    "Election type"
lab var flag_wrong_sum_seats		"Sum of elected seats in parliament is inconsistent with the total number of seats"
lab var flag_inconsequential		"Flags cancelled or inconsequential elections"
lab var flag_inconsequential_note		"Why flag_inconsequential is equal to 1"
lab var flag_coup		"Flags elections that were shortly followed by a coup or revolution"
lab var flag_vacant_seats		"Flags elections which left vacant seats in parliament"
lab var flag_vacant_seats_nb		"Number of vacant seats in parliament"
lab var flag_appointed		"Flags elections in which some seats were not elected directly"
lab var flag_appointed_nb		"Number of seats for which MPs were not elected directly"
lab var flag_non_partisan		"Flags elections with no parties"
lab var Total_Seats			"Total number of seats"
forvalues i=1/75 {
	lab var Party_`i'        "Party or coalition n°`i'"
	lab var Seats_`i'  "Seats won by party n°`i'"
	lab var Seat_Share_`i'  "Seat share of party n°`i'"
	replace Seat_Share_`i' = 100*Seat_Share_`i'
}

compress
format Country %30s
format Type_Election %15s
format Party_* %25s
recast double Seat_Share_*

save "$project_path/data/2_intermediary/elections/Parliamentary elections/parl_elec_global", replace
